package com.csu.jpetstore_reconfiguration_back.Controller;

import com.csu.jpetstore_reconfiguration_back.common.CommonResponse;
import com.csu.jpetstore_reconfiguration_back.service.CartService;
import com.csu.jpetstore_reconfiguration_back.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RestController;
import com.csu.jpetstore_reconfiguration_back.domain.Cart;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
public class CartController {
    @Autowired
    CartService cartService;


    //获取购物车内商品列表
    @GetMapping("/carts")
    @ResponseBody
    public CommonResponse<List<CartVO>> getCartItems(HttpServletRequest request)
    {
        return cartService.getCartItems(request);
    }

    //查找当前用户的购物车
    @PostMapping("/carts/currentUser")
    @ResponseBody
    public CommonResponse<List<CartVO>> getCartItemsByUserId(HttpServletRequest request)
    {
        return cartService.getCartItemsByUserId(request);
    }

    //删除购物车内商品
    @GetMapping("/carts/{id}/delete")
    @ResponseBody
    public CommonResponse<CartVO> deleteCartItem(@PathVariable("id") String id,HttpServletRequest request)
    {
        return cartService.deleteCartItem(id,request);
    }
    //添加购物车产品
    @PostMapping("/carts")
    public CommonResponse<CartVO> addCart(@Validated @RequestBody Cart cart, HttpServletRequest request)
    {
        return cartService.addCart(cart,request);
    }
    //修改购物车产品
    @PostMapping("/cart/update")
    public  CommonResponse<CartVO> updateCart(@Validated @RequestBody Cart cart, HttpServletRequest request){
        return cartService.updateCart(cart,request);
    }
    @PostMapping("/carts/{id}")
    public  CommonResponse<CartVO> updateCart(@PathVariable String id,@Validated @RequestBody Cart cart, HttpServletRequest request)
    {
        return cartService.updateCart(cart,request);
    }
}
